package de.bensoft.jrail.client.ui.trackplan.segments;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;

import javax.inject.Inject;

import de.bensoft.jrail.business.entity.BlockData;
import de.bensoft.jrail.business.entity.TrackplanSegment;
import de.bensoft.jrail.client.common.FormCreator;
import de.bensoft.jrail.client.common.SceneCreatorResult;
import de.bensoft.jrail.client.ui.trackplan.TrackplanController;
import de.bensoft.jrail.client.ui.trackplan.blockadmin.BlockAdminController;

public class BlockTrackSegment extends BasicTrackSegment {

	private ContextMenu contextMenu;
	
	@Inject
	TrackplanController trackplanController;
	
	@Inject
	FormCreator formCreator;
	
	private static final Long DEFAULT_DIMENSION = 3l;
	
	private Orientation orientation;	
	public Orientation getOrientation() { return orientation; }
	public void setOrientation(Orientation orientation) { this.orientation = orientation; doSize(); }

	private Long blockDimension;
	public Long getBlockDimension() { return blockDimension; }
	public void setBlockDimension(Long blockDimension) { this.blockDimension = blockDimension; }
	
	private StringProperty nameProperty;	
	public StringProperty nameProperty() { return nameProperty; }
	
	private Label mainLabel;
	
	public BlockTrackSegment() {
		
		this.nameProperty = new SimpleStringProperty();
		
		this.getStyleClass().add("blockTrackSegment");
		this.blockDimension = DEFAULT_DIMENSION;
		this.orientation = Orientation.HORIZONTAL;
		removeGraphic();
		
		
		// Contextmenu
		contextMenu = new ContextMenu();
		MenuItem mi1 = new MenuItem("Properties");
		contextMenu.getItems().addAll(mi1);
		
		// Text
		mainLabel = new Label();
		mainLabel.textProperty().bindBidirectional(nameProperty);
		this.getChildren().add(mainLabel);
				
		mi1.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent arg0) {
				showBlockAdmin();
			}
		});
	}
			
	// prevent sizing to one cell
	@Override
	public void setSize(Double size) {
		this.size = size;
		doSize();
	}
	
	private void doSize() {
		double width = this.size * blockDimension;
		
		this.setPrefSize(width, size);
		
		// Remove overlapping segments
		for(int i=0; i < blockDimension - 1; i++) {
			if(orientation.equals(Orientation.HORIZONTAL)) {
				trackplanController.removeSegment(getColumn() + i + 1, getRow());
			} else {
				trackplanController.removeSegment(getColumn(), getRow()  + i + 1);
			}
		}
		
		mainLabel.setPrefWidth(width);
		mainLabel.setPrefHeight(size);
		mainLabel.setAlignment(Pos.CENTER);
		
	}
	
	private void showBlockAdmin() {
		SceneCreatorResult res = formCreator.createScene(BlockAdminController.class);
		BlockAdminController controller = (BlockAdminController)res.getController();
			
		controller.setSegment(this);
		
		Stage stage = new Stage();
		stage.setScene(res.getScene());
		stage.setTitle("Blockadmin");
		stage.setResizable(false);
		stage.sizeToScene();
		stage.initModality(Modality.APPLICATION_MODAL);
		stage.show();
	}
	
	@Override
	public void onClicked(MouseEvent event) {
		super.onClicked(event);
		
		if(event.getButton().equals(MouseButton.SECONDARY)) {
			contextMenu.show(this, Side.RIGHT, 0, 0);
		}
	}
	
	
	private void provideBlockData() {
		if(getSegmentData().getBlockData() == null) {
			BlockData blockData = new BlockData();
			blockData.setName("Unknown");
			
			getSegmentData().setBlockData(blockData);
		} 
		
			
		nameProperty.set(getSegmentData().getBlockData().getName());
	}
	
	
	
	@Override
	public void setSegmentData(TrackplanSegment segmentData) {
		super.setSegmentData(segmentData);
		provideBlockData();
	}
}
